﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForCheckedListBox.cs
 * Module Version:  1.0.0 (2012-5-25)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension winformcontrol instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Windows.Forms;

namespace UCF.Core.Forms
{
    /// <summary>
    /// CheckedListBox控件扩展方法
    /// </summary>
    public static class ExMethodForCheckedListBox
    {
        /// <summary>
        /// 设置全选或取消全选
        /// </summary>
        /// <param name="c"></param>
        /// <param name="checkedstate"></param>
        public static void SetItemsChecked(this CheckedListBox c, bool checkedstate)
        {
            int i = c.Items.Count - 1;
            while (i >= 0)
            {
                c.SetItemChecked(i, checkedstate);
                i--;
            }
        }

        /// <summary>
        /// 选中项目
        /// </summary>
        /// <param name="c"></param>
        /// <param name="checkedstate"></param>
        public static void SetItemsChecked(this CheckedListBox c, params object[] items)
        {
            if (items.Length == 0 || c.Items.Count == 0) return;
            for (int i = 0; i < c.Items.Count; i++)
            {
                if (c.Items[i].GetType() == typeof(DataRowView) || c.Items[i].GetType() == typeof(DataRow))
                {
                    if (items.Contains(((DataRowView)c.Items[i])[0].ToString())) c.SetItemChecked(i, true);
                }
                else
                {
                    if (items.Contains(c.Items[i])) c.SetItemChecked(i, true);
                }
            }
        }

        /// <summary>
        /// 设置反选
        /// </summary>
        /// <param name="c"></param>
        public static void SetItemsInverseChecked(this CheckedListBox c)
        {
            int i = c.Items.Count - 1;
            while (i >= 0)
            {
                c.SetItemChecked(i, !(c.GetItemCheckState(i) == CheckState.Checked));
                i--;
            }
        }

        /// <summary>
        /// 获取值列表
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <param name="c"></param>
        /// <returns></returns>
        public static IList<V> GetCheckedValueList<V>(this CheckedListBox c)
        {
            IList<V> list = new List<V>();
            foreach (object s in c.CheckedItems)
            {
                if (s.GetType() == typeof(DataRow) || s.GetType() == typeof(DataRowView))
                {
                    list.Add((V)((DataRowView)s)[0]);
                }
                else
                {
                    list.Add((V)s);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取名称列表
        /// </summary>
        /// <typeparam name="V"></typeparam>
        /// <param name="c"></param>
        /// <returns></returns>
        public static IList<V> GetCheckedTextList<V>(this CheckedListBox c)
        {
            IList<V> list = new List<V>();
            foreach (object s in c.CheckedItems)
            {
                if (s.GetType() == typeof(DataRow) || s.GetType() == typeof(DataRowView))
                {
                    list.Add((V)((DataRowView)s)[1]);
                }
                else
                {
                    list.Add((V)s);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取单选按钮组的值
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static string GetRadioButtonGroupValue(this IList<RadioButton> arr)
        {
            foreach (RadioButton c in arr)
            {
                if (c.Checked) return c.Tag == null ? string.Empty : c.Tag.ToString();
            }
            return string.Empty;
        }

        /// <summary>
        /// 设置单选按钮组的值
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="v"></param>
        public static void SetRadioButtonGroupValue(this IList<RadioButton> arr, object v)
        {
            if (v == null) return;
            foreach (RadioButton c in arr)
            {
                if (c.Tag != null && c.Tag.ToString() == v.ToString())
                {
                    c.Checked = true;
                    break;
                }
            }
        }
    }
}
